<!DOCTYPE html>
<html>
  <head>
     
    <meta charset="UTF-8">
    <title>Oracle AES加解密Demo - 我的元宇宙</title>
    <link rel="shortcut icon" href="/static/img/icon.png">
    <link rel="icon" href="/static/img/icon.png" sizes="192x192"/>
    
<link rel="stylesheet" href="/static/kico.css">
<link rel="stylesheet" href="/static/hingle.css">

    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/FortAwesome/Font-Awesome/css/font-awesome.min.css">

    <meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1"/>
    <meta property="og:site_name" content="我的元宇宙">
    <meta property="og:title" content="Oracle AES加解密Demo"/>
    
 
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <header>
    <div class="head-title">
        <h4>我的元宇宙</h4>
    </div>
    <div class="head-action">
        <div class="toggle-btn"></div>
        <div class="light-btn"></div>
        <div class="search-btn"></div>
    </div>
    <form class="head-search" method="post">
        <input type="text" name="s" placeholder="搜索什么？">
    </form>
    <nav class="head-menu">
        <a href="/">首页</a>
        <div class="has-child">
            <a>分类</a>
            <div class="sub-menu">
                <a class="category-link" href="/categories/%E5%AD%A6%E4%B9%A0/">学习</a><a class="category-link" href="/categories/%E6%9D%82%E9%A1%B9/">杂项</a><a class="category-link" href="/categories/%E7%AC%94%E8%AE%B0/">笔记</a><a class="category-link" href="/categories/%E8%BD%AC%E8%BD%BD/">转载</a>
            </div>
        </div>
        
            <a href="/about">关于我</a>
        
    </nav>
</header>

    <main>
    <div class="wrap min">
        <section class="post-title">
            <h2>Oracle AES加解密Demo</h2>
            <div class="post-meta">
                <time class="date">2021.11.05</time>
            
                <span class="category"><a class="category-link" href="/categories/%E7%AC%94%E8%AE%B0/">笔记</a>，<a class="category-link" href="/categories/%E7%AC%94%E8%AE%B0/Oracle/">Oracle</a></span>
            
            </div>
        </section>
        <article class="post-content">
        
            <h2 id="Oracle-AES加解密Demo"><a href="#Oracle-AES加解密Demo" class="headerlink" title="Oracle AES加解密Demo"></a>Oracle AES加解密Demo</h2><ol>
<li>近期要使用Apex与其他系统做对接其中有一些敏感字段,已经加密.所以要使用<code>Oracle</code>解密. 以下是对应PL/SQL的Demo代码</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace PACKAGE CRYPTO_AES_PKG <span class="keyword">IS</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    -- 使用demo</span></span><br><span class="line"><span class="comment">    declare</span></span><br><span class="line"><span class="comment">        l_key  varchar2(200) := &#x27;aaaaaaaa&#x27;;</span></span><br><span class="line"><span class="comment">        l_data varchar2(200) := &#x27;123456&#x27;;</span></span><br><span class="line"><span class="comment">        l_en   varchar2(400);</span></span><br><span class="line"><span class="comment">        l_de   varchar2(400);</span></span><br><span class="line"><span class="comment">    begin</span></span><br><span class="line"><span class="comment">        --    加密</span></span><br><span class="line"><span class="comment">        l_en := CRYPTO_AES_PKG.F_ENCRYPT(l_data, l_key);</span></span><br><span class="line"><span class="comment">        DBMS_OUTPUT.PUT_LINE(l_en); -- FP7MsKoQifAW311/XPIzLQ==</span></span><br><span class="line"><span class="comment">        --    解密</span></span><br><span class="line"><span class="comment">        l_de := CRYPTO_AES_PKG.F_DECRYPT(l_en, l_key);</span></span><br><span class="line"><span class="comment">        DBMS_OUTPUT.PUT_LINE(l_de);  -- 123456</span></span><br><span class="line"><span class="comment">    end;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">--    加密</span></span><br><span class="line">    <span class="keyword">FUNCTION</span> F_ENCRYPT(I_INPUT_STRING VARCHAR2, I_KEY_STRING varchar2) <span class="keyword">RETURN</span> VARCHAR2;</span><br><span class="line">    <span class="comment">--    解密</span></span><br><span class="line">    <span class="keyword">FUNCTION</span> F_DECRYPT(I_INPUT_STRING VARCHAR2, I_KEY_STRING VARCHAR2) <span class="keyword">RETURN</span> VARCHAR2;</span><br><span class="line"><span class="keyword">END</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace PACKAGE BODY CRYPTO_AES_PKG <span class="keyword">IS</span></span><br><span class="line">    <span class="comment">--加密</span></span><br><span class="line">    <span class="keyword">FUNCTION</span> F_ENCRYPT(I_INPUT_STRING VARCHAR2, I_KEY_STRING varchar2) <span class="keyword">RETURN</span> VARCHAR2</span><br><span class="line">        <span class="keyword">IS</span></span><br><span class="line">        V_KEY_STRING    RAW(<span class="number">128</span>)    :<span class="operator">=</span> utl_raw.cast_to_raw(</span><br><span class="line">                sys.dbms_obfuscation_toolkit.md5(input_string <span class="operator">=</span><span class="operator">&gt;</span> I_KEY_STRING)); <span class="comment">-- 加密串,同时也是解密串</span></span><br><span class="line">        V_ENCRYPTED_RAW RAW(<span class="number">200</span>);</span><br><span class="line">        encryption_type pls_integer :<span class="operator">=</span> DBMS_Crypto.ENCRYPT_AES128 <span class="operator">+</span> DBMS_Crypto.CHAIN_ECB <span class="operator">+</span> DBMS_Crypto.PAD_PKCS5;</span><br><span class="line">    <span class="keyword">BEGIN</span></span><br><span class="line">        V_ENCRYPTED_RAW :<span class="operator">=</span> DBMS_CRYPTO.ENCRYPT(</span><br><span class="line">                SRC <span class="operator">=</span><span class="operator">&gt;</span> UTL_I18N.STRING_TO_RAW(I_INPUT_STRING, <span class="string">&#x27;AL32UTF8&#x27;</span>),<span class="comment">-- 被加密的字符串</span></span><br><span class="line">                TYP <span class="operator">=</span><span class="operator">&gt;</span> encryption_type, <span class="comment">-- DBMS_CRYPTO.AES_CBC_PKCS5 -- 加密算法，算法有很多</span></span><br><span class="line">                KEY <span class="operator">=</span><span class="operator">&gt;</span> V_KEY_STRING);</span><br><span class="line">        <span class="comment">-- 加密串</span></span><br><span class="line">        <span class="comment">--         RETURN (RAWTOHEX(V_ENCRYPTED_RAW));</span></span><br><span class="line">        <span class="keyword">RETURN</span> (UTL_I18N.RAW_TO_CHAR(utl_encode.base64_encode(RAWTOHEX(V_ENCRYPTED_RAW))));</span><br><span class="line">    <span class="keyword">END</span> F_ENCRYPT;</span><br><span class="line">    <span class="comment">--解密</span></span><br><span class="line">    <span class="keyword">FUNCTION</span> F_DECRYPT(I_INPUT_STRING VARCHAR2, I_KEY_STRING VARCHAR2) <span class="keyword">RETURN</span> VARCHAR2</span><br><span class="line">        <span class="keyword">IS</span></span><br><span class="line">        V_KEY_STRING    RAW(<span class="number">128</span>)    :<span class="operator">=</span> utl_raw.cast_to_raw(</span><br><span class="line">                sys.dbms_obfuscation_toolkit.md5(input_string <span class="operator">=</span><span class="operator">&gt;</span> I_KEY_STRING));<span class="comment">-- 加密串,同时也是解密串</span></span><br><span class="line">        V_DECRYPTED_RAW RAW(<span class="number">200</span>);</span><br><span class="line">        b64_de          RAW(<span class="number">200</span>);</span><br><span class="line">        encryption_type pls_integer :<span class="operator">=</span> DBMS_Crypto.ENCRYPT_AES128 <span class="operator">+</span> DBMS_Crypto.CHAIN_ECB <span class="operator">+</span> DBMS_Crypto.PAD_PKCS5;</span><br><span class="line">    <span class="keyword">BEGIN</span></span><br><span class="line">        b64_de :<span class="operator">=</span> utl_encode.base64_decode(utl_raw.cast_to_raw(I_INPUT_STRING));</span><br><span class="line">        V_DECRYPTED_RAW :<span class="operator">=</span> DBMS_CRYPTO.DECRYPT(</span><br><span class="line">                SRC <span class="operator">=</span><span class="operator">&gt;</span> HEXTORAW(b64_de),</span><br><span class="line">                TYP <span class="operator">=</span><span class="operator">&gt;</span>encryption_type,</span><br><span class="line">                KEY <span class="operator">=</span><span class="operator">&gt;</span> V_KEY_STRING);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">RETURN</span> (</span><br><span class="line"><span class="comment">--             UTL_RAW.CAST_TO_VARCHAR2(V_DECRYPTED_RAW)</span></span><br><span class="line">            UTL_I18N.RAW_TO_CHAR(V_DECRYPTED_RAW, <span class="string">&#x27;AL32UTF8&#x27;</span>)</span><br><span class="line">            );</span><br><span class="line">    <span class="keyword">END</span> F_DECRYPT;</span><br><span class="line"><span class="keyword">END</span> CRYPTO_AES_PKG;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>附带一个Python的Demo</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> base64</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"><span class="keyword">from</span> Crypto.Cipher <span class="keyword">import</span> AES</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AESDecryptDBPassWD</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, key, signature</span>):</span></span><br><span class="line">        self.md5 = hashlib.md5(key.encode())</span><br><span class="line">        self.key = key</span><br><span class="line">        self.signature = signature</span><br><span class="line">        self.block_size = <span class="number">16</span></span><br><span class="line">        self.pad = <span class="keyword">lambda</span> passwd: passwd + (self.block_size - <span class="built_in">len</span>(passwd) % self.block_size) * <span class="built_in">chr</span>(</span><br><span class="line">            self.block_size - <span class="built_in">len</span>(passwd) % self.block_size)</span><br><span class="line">        self.unpad = <span class="keyword">lambda</span> passwd: passwd[:-<span class="built_in">ord</span>(passwd[<span class="built_in">len</span>(passwd) - <span class="number">1</span>:])]</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">decrypt</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            signature = base64.b64decode(self.signature)</span><br><span class="line">            aes = AES.new(self.md5.digest(), AES.MODE_ECB)</span><br><span class="line">            <span class="keyword">return</span> self.unpad(aes.decrypt(signature)).decode()</span><br><span class="line">        <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">encrypt</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            aes = AES.new(self.md5.digest(), AES.MODE_ECB)</span><br><span class="line">            passwd = aes.encrypt(self.pad(self.signature))</span><br><span class="line">            <span class="keyword">return</span> base64.b64encode(passwd)</span><br><span class="line">        <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    signature = <span class="string">&#x27;FP7MsKoQifAW311/XPIzLQ==&#x27;</span></span><br><span class="line">    aes = AESDecryptDBPassWD(<span class="string">&#x27;aaaaaaaa&#x27;</span>, signature)</span><br><span class="line">    password = aes.decrypt()</span><br><span class="line">    <span class="keyword">assert</span> password == <span class="string">&#x27;123456&#x27;</span></span><br><span class="line">    <span class="built_in">print</span>(password)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

        </article>
        <section class="post-near">
            <ul>
                
                    <li>上一篇: <a href="/2021/11/05/b209999e9b35/">Apex对接钉钉免登</a></li>
                
                
                    <li>下一篇: <a href="/2021/11/03/6c7a14fc9a24/">Apex安装及配置</a></li>
                
            </ul>
        </section>
        
            <section class="post-tags">
            <a class="-none-link" href="/tags/AES/" rel="tag">AES</a><a class="-none-link" href="/tags/Apex/" rel="tag">Apex</a><a class="-none-link" href="/tags/Oracle/" rel="tag">Oracle</a><a class="-none-link" href="/tags/PL-SQL/" rel="tag">PL/SQL</a><a class="-none-link" href="/tags/%E5%85%83%E5%AE%87%E5%AE%99/" rel="tag">元宇宙</a>
            </section>
        
    
        <section class="post-author">
        
            <figure class="author-avatar">
                <!-- <img src="https://metaverse-1256174970.cos.ap-shanghai.myqcloud.com/image_%2Fahmed-mulla-hS-2bYjeyWc-unsplash.jpg" alt="Pan" /> -->
                <img src="https://nuczmjfhvndrjhrfpqkcyeimmdrcsebw.oss-cn-shanghai.aliyuncs.com/jbfffurvdqmpvhukbzbsea/QPWp3GjspHEz8QHi6RDm.jpeg" alt="Pan" >
            </figure>
        
            <div class="author-info">
                <h4>Pan</h4>
                <p>社会主义螺丝钉,币圈韭菜,A股韭菜</p>
            </div>
        </section>
    
    </div>
</main>

    <footer>
    <div class="buttons">
        <a class="to-top" href="#"></a>
    </div>
    <div class="wrap min">
        <section class="widget">
            <div class="row">
                <div class="col-m-4">
                    <h3 class="title-recent">最新文章：</h3>
                    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2022/05/01/0586406b1d50/">不同衣物收纳教程</a></li><li class="post-list-item"><a class="post-list-link" href="/2022/01/13/6578eb87a21b/">Apex直传阿里云OSS</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/12/13/3449662a057f/">Oracle Apex IR 多选批量处理后页面局部刷新</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/11/05/60190b5049e6/">Apex上传文件至Windows网盘</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/11/05/b209999e9b35/">Apex对接钉钉免登</a></li><li class="post-list-item"><a class="post-list-link" href="/2021/11/05/e7f888284497/">Oracle AES加解密Demo</a></li></ul>
                </div>
                <div class="col-m-4">
                    <h3 class="title-date">时光机：</h3>
                    <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">五月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">一月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/12/">十二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">十一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">六月 2020</a></li></ul>
                </div>
                <div class="col-m-4">
                    <h3 class="title-tags">标签云：</h3>
                    <a href="/tags/AES/" style="font-size: 10px;">AES</a> <a href="/tags/Apex/" style="font-size: 16.67px;">Apex</a> <a href="/tags/CSS/" style="font-size: 10px;">CSS</a> <a href="/tags/HTML/" style="font-size: 10px;">HTML</a> <a href="/tags/JS/" style="font-size: 10px;">JS</a> <a href="/tags/Nginx/" style="font-size: 10px;">Nginx</a> <a href="/tags/OSS/" style="font-size: 10px;">OSS</a> <a href="/tags/Oracle/" style="font-size: 13.33px;">Oracle</a> <a href="/tags/Ords/" style="font-size: 10px;">Ords</a> <a href="/tags/PL-SQL/" style="font-size: 10px;">PL/SQL</a> <a href="/tags/SQLcl/" style="font-size: 10px;">SQLcl</a> <a href="/tags/Tomcat/" style="font-size: 10px;">Tomcat</a> <a href="/tags/Windows/" style="font-size: 10px;">Windows</a> <a href="/tags/%E5%85%83%E5%AE%87%E5%AE%99/" style="font-size: 20px;">元宇宙</a> <a href="/tags/%E5%A4%9C%E8%AF%BB/" style="font-size: 13.33px;">夜读</a> <a href="/tags/%E6%9D%82%E9%A1%B9/" style="font-size: 10px;">杂项</a> <a href="/tags/%E7%BD%91%E7%9B%98/" style="font-size: 10px;">网盘</a> <a href="/tags/%E8%BD%AC%E8%BD%BD/" style="font-size: 13.33px;">转载</a> <a href="/tags/%E9%92%89%E9%92%89/" style="font-size: 10px;">钉钉</a> <a href="/tags/%E9%98%BF%E9%87%8C%E4%BA%91/" style="font-size: 10px;">阿里云</a>
                </div>
            </div>
        </section>
        <section class="sub-footer">
            <p>友链:<a href="https://wangfanggang.com/" target="_blank">钢钢更新</a></p>
            <p><span>用于分享个人成长经历,日常状况.心情感悟,分享Apex开发经验,Python开发经验,Go等一系列学习笔记.作为网络知识储备.也会记录一些网络金句</span></p>
            <p>© 2022 
                <a href="/">我的元宇宙</a>. All Rights Reserved. Theme By 
                <a href="https://github.com/Dreamer-Paul/Hingle" target="_blank" rel="nofollow">Hingle</a>.
                <a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">豫ICP备19030991号</a></p>
        </section>
    </div>
    
</footer>


<script src="/static/kico.js"></script>
<script src="/static/hingle.js"></script>


<script>var hingle = new Paul_Hingle({"copyright":true,"night":true});</script>

  </body>
</html>
